package com.woniuxy.controller;

import com.woniuxy.Exception.serviceException;
import com.woniuxy.entity.Equipment;
import com.woniuxy.entity.PageBean;
import com.woniuxy.entity.ResponseResult;
import com.woniuxy.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EquipmentController {
    @Autowired
    EquipmentService equipmentService;

    //路径变量参数
    //equipment/{pageIndex}/{pageSize}
    //查询所有设备信息
    //通过输入设备名称查询
    @PostMapping("/equipment/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<Equipment>> getAllEquipment(@PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize,
                                                    @RequestBody Equipment equipment) {
        ResponseResult<PageBean<Equipment>> resEquipment;
        try {
            PageBean<Equipment> pageBean = equipmentService.getEquipmentPageByCondition(equipment, pageIndex, pageSize);
            resEquipment = new ResponseResult<>(200, "ok", pageBean);

            return resEquipment;
        } catch (serviceException e) {
            resEquipment = new ResponseResult<>(500, e.getMessage(), null);
            return resEquipment;
        }
    }

    //通过设备种类查询
    @PostMapping("/equipmentByCategory/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<Equipment>> getAllEquipmentByCategory(@PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize,
                                                    @RequestBody Equipment equipment) {
        ResponseResult<PageBean<Equipment>> resEquipment;
        try {
            PageBean<Equipment> pageBean = equipmentService.getEquipmentPageByCategory(equipment, pageIndex, pageSize);
            resEquipment = new ResponseResult<>(200, "ok", pageBean);

            return resEquipment;
        } catch (serviceException e) {
            resEquipment = new ResponseResult<>(500, e.getMessage(), null);
            return resEquipment;
        }
    }

    //通过设备名称查询
    @PostMapping("/equipment/")
    public List<Equipment> getEquipment(@RequestBody Equipment equipment) {
        List<Equipment> equipments = equipmentService.getEquipmentByName(equipment);
        return equipments;
        }

}
